package com.spe.shangcheng.search.crontroller;

import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author ：spe
 * @description：TODO
 * @date ：2024/11/25 20:30
 */
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;

@RestController
@Slf4j
@RequestMapping("search")
public class EsController {

    @Resource
    private ProductElasticsearchRepository repository;
    @Resource
    private EsStudentRepository studentRepository;

    @GetMapping("addStudent/{id}")
    public Student addStudent(@PathVariable(name = "id",required = true) String id) {
        Student student=new Student();
        student.setSId(id);
        student.setSName("张三");
        student.setSAge(10);
        student.setSAddress("第九街区");
        student.setSCreateTime(new Date());
        student.setSCourseList(new String[]{"a","b"});
        student.setSColorList(Arrays.asList("c","d"));
        return studentRepository.save(student);
    }

    @GetMapping("add/{id}")
    public Product add(@PathVariable(name = "id",required = true) Long id) {
        return repository.save(Product.builder()
                .id(id)
                .name("商品" + id)
                .price(99 + id.intValue())
                .description("超级简介咯咯咯" + id).build());
    }

    @GetMapping("findStudentById")
    public Student findStudentById(){
        return studentRepository.findById("1").get();
    }
    @GetMapping("findStudentALL")
    public Iterable<Student> findStudentALL(){
        return studentRepository.findAll();
    }

    @GetMapping("findAll")
    public Iterable<Product> findAll() {
        return repository.findAll();
    }

    @GetMapping("findPage/{page}/{size}")
    public Iterable<Product> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return repository.findAll(PageRequest.of(page, size, Sort.by("id").descending()));
    }

    @GetMapping("findByNameLike/{page}/{size}/{name}")
    public Iterable<Product> findByNameLike(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @PathVariable("name") String name) {
        return repository.findByNameLike(name, PageRequest.of(page, size, Sort.by("id").descending()));
    }

}

